<template>
  <view class="pet-container" v-if="state">
    <view class="pet-list">
      <view class="pet-item" v-for="(pet, index) in pets" :key="index" @click="goToEditPet(pet)">
        <view class="pet-content">
          <view class="pet-avatar-container">
            <image class="pet-avatar" :src="pet.img" mode="aspectFill"></image>
          </view>
          <view class="pet-info">
            <text class="pet-name">{{ pet.name }}</text>
            <text class="pet-type">{{ pet.pettype_name }}（{{ pet.age }}岁）</text>
          </view>
        </view>
      </view>
    </view>
    <view class="button-section">
      <button class="add-pet" @click="goToAddPet">新增宠物</button>
    </view>
  </view>
  <view v-else>
    <view class="loading">加载中...</view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      pets: [],
      selectedPetId: null,
      state: 0,
    };
  },
  onLoad() {
    this.fetchPets();
  },
  methods: {
    async fetchPets() {
      const token = uni.getStorageSync('Authorization');
      if (!token) {
        return;
      }
      const url = 'http://localhost:8887/userpet/selectUserpet';
      const header = {
        'Authorization': token,
      };
      const res = await uni.request({
        url,
        method: 'GET',
        header,
      });
      if (res.data.code === 200) {
        this.state = 1;
        this.pets = res.data.data;
      } else {
        this.state = 0;
        uni.showToast({
          title: '获取用户宠物信息失败',
          icon: 'none',
        });
      }
    },
    goToEditPet(pet) {
      uni.navigateTo({
        url: '/pages/userpetadd/userpetadd',
        success: (res) => {
          res.eventChannel.emit('passData', {
            pet: JSON.stringify(pet),
            action: '编辑'
          });
        }
      });
    },
    goToAddPet() {
      uni.navigateTo({
        url: '/pages/userpetadd/userpetadd',
        success: (res) => {
          res.eventChannel.emit('passData', { action: '新增' });
        }
      });
    },
  },
};
</script>

<style>
.pet-container {
  padding: 10px;
}
.pet-list {
  margin-bottom: 50px;
}
.pet-item {
  display: flex;
  align-items: center;
  height: 100px; /* Adjusted height */
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 8px;
  margin-bottom: 10px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.pet-content {
  display: flex;
  width: 100%;
}
.pet-avatar-container {
  margin-right: 15px;
}
.pet-avatar {
  width: 60px; /* Adjusted size */
  height: 60px; /* Adjusted size */
  border-radius: 50%;
}
.pet-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
  flex-grow: 1;
}
.pet-name {
  font-size: 18px;
  font-weight: bold;
}
.pet-type {
  font-size: 14px;
  color: #555;
}
.add-pet {
  position: fixed;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 40px;
  background-color: red;
  color: #fff;
  border-radius: 20px;
  border: none;
}
</style>